package com.caregiver.watch.business.care.model.vo;

import com.caregiver.watch.business.care.enums.PackageStatusEnum;
import com.caregiver.watch.business.care.enums.PackageTypeEnum;
import com.caregiver.watch.business.care.enums.PackageUnitEnum;
import com.caregiver.watch.business.care.model.po.UserPackageOrder;
import com.caregiver.watch.common.utils.number.IntegerUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.commons.lang3.BooleanUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;

import static com.caregiver.watch.business.care.enums.PackageStatusEnum.EFFECTIVE;
import static com.caregiver.watch.business.care.enums.PackageStatusEnum.EXPIRED;
import static com.caregiver.watch.business.care.enums.PackageStatusEnum.PENDING;
import static com.caregiver.watch.common.constant.NumberConstants.ZERO;

/**
 * @Description: 已经购买的套餐信息
 * @Author 疆戟
 * @Date 2025/10/13 22:46
 * @Version 1.0
 */
@Data
@Schema(description = "已经购买的套餐信息")
public class BoughtPackageInfoVo {

    @Schema(description = "套餐名称")
    private String packageName;

    @Schema(description = "套餐类型")
    private PackageTypeEnum packageType;

    @Schema(description = "支持的设备数量")
    private Integer supportDeviceNum;

    @Schema(description = "套餐价格")
    private BigDecimal packagePrice;


    @Schema(description = "套餐值")
    private Integer packageValue;


    @Schema(description = "套餐单位 COUNT 次 DAY 天 MONTH 月 YEAR 年")
    private PackageUnitEnum packageUnit;


    @Schema(description = "状态(1.待生效, 2.生效中, 3.已失效)")
    private PackageStatusEnum status;

    /**
     * 套餐有效开始时间
     */
    @Schema(description = "套餐有效开始时间")
    private LocalDateTime validStartTime;

    /**
     * 套餐有效结束时间
     */
    @Schema(description = "套餐有效结束时间")
    private LocalDateTime validEndTime;


    @Schema(description = "套餐购买时间")
    private LocalDateTime buyTime;

    @Schema(description = "套餐剩余次数")
    private Integer leftCount;

    public static BoughtPackageInfoVo build(UserPackageOrder order) {
        if (Objects.isNull(order)) {
            return null;
        }
        BoughtPackageInfoVo vo = new BoughtPackageInfoVo();
        vo.packageName = order.getPackageName();
        vo.packageType = order.getPackageType();
        vo.supportDeviceNum = order.getSupportDeviceNum();
        vo.packagePrice = order.getPackagePrice();
        vo.packageValue = order.getPackageValue();
        vo.packageUnit = order.getPackageUnit();
        vo.validStartTime = order.getEffectiveTime();
        vo.validEndTime = order.getExpireTime();
        vo.buyTime = order.getOrderTime();
        LocalDateTime now = LocalDateTime.now();
        // 判断是否生效
        Boolean isEffective = now.isAfter(order.getEffectiveTime());
        if (order.getPackageType() == PackageTypeEnum.COUNT) {
            vo.leftCount = IntegerUtils.ofNullable(order.getPackageValue()) - IntegerUtils.ofNullable(order.getWarningCount());
        } else {
            vo.leftCount = order.getPackageValue();
        }

        if (BooleanUtils.isTrue(isEffective)) {
            // 套餐状态
            if (order.getPackageType() == PackageTypeEnum.COUNT) {
                vo.setStatus(vo.leftCount > ZERO ? EFFECTIVE : EXPIRED);
            } else {
                vo.setStatus(BooleanUtils.isTrue(now.isBefore(order.getExpireTime())) ? EFFECTIVE : EXPIRED);
            }
        } else {
            vo.setStatus(PENDING);
        }
        return vo;
    }
}
